<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="店铺ID" prop="cashier">
            <el-input v-model="queryParams.shopId" placeholder="请输入店铺ID" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="收银员ID" prop="cashier">
            <el-input v-model="queryParams.cashier" placeholder="请输入收银员ID" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="账号名称" prop="accountName">
            <el-input v-model="queryParams.accountName" placeholder="请输入账号名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="面值金额" prop="faceValue">
            <el-input v-model="queryParams.faceValue" placeholder="请输入面值金额" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['retail:shopCashierQrcode:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['retail:shopCashierQrcode:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="shopCashierQrcodeList" @selection-change="handleSelectionChange" class="tableEmpty">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="店铺ID" align="center" prop="shopId" />
        <el-table-column label="收银员ID" align="center" prop="cashier" />
        <el-table-column label="账号名称" align="center" prop="accountName" />
        <el-table-column label="二维码" align="center" prop="qrcode" />
        <el-table-column label="二维码圖" align="center" prop="qrcodeImage">
          <template #default="scope">
            <span> <el-icon @click="showQrcode(scope.row)"><grid /></el-icon> </span>
          </template>
        </el-table-column>
        <el-table-column label="二维码类型" align="center" prop="qrcodeType" />
        <el-table-column label="二维码备注" align="center" prop="qrcodeNote">
          <template #default="scope">
            {{ scope.row.qrcodeNote==null?"--":scope.row.qrcodeNote }}
          </template>
        </el-table-column>
        <el-table-column label="面值金额" align="center" prop="faceValue">
          <template #default="scope">
            {{ scope.row.faceValue / 100 }}
          </template>
        </el-table-column>
        <el-table-column label="日使用限制" align="center" prop="usableTimes" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['retail:shopCashierQrcode:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="360px" append-to-body>
      <el-card>
        <el-row class="mt-8px" :gutter="8" justify="space-between">
          <el-col :xl="16" :lg="16" :md="24" :sm="24" :xs="24" class="mb-8px">
            <div>
            <!--<canvas id="canvas" ref="canvasRef" />-->
              <span><el-link type="primary">{{ qrcodeUrl }}</el-link></span>
              <el-image :src="qrCodeContent" v-if="qrCodeContent" />
            </div>
          </el-col>
        </el-row>
        <!--<el-row>
          <el-link type="primary">{{ qrCodeContent }}</el-link>
        </el-row>-->
      </el-card>
    </el-dialog>
  </div>
</template>

<script setup name="ShopCashierQrcode" lang="ts">
import { listShopCashierQrcode, getShopCashierQrcode, delShopCashierQrcode, addShopCashierQrcode, updateShopCashierQrcode } from '@/api/retail/shopCashierQrcode';
import { ShopCashierQrcodeVO, ShopCashierQrcodeQuery, ShopCashierQrcodeForm } from '@/api/retail/shopCashierQrcode/types';
import {Grid} from "@element-plus/icons-vue";
import qrcode from "qrcode";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_normal_disable } = toRefs<any>(proxy?.useDict('sys_normal_disable'));

const shopCashierQrcodeList = ref<ShopCashierQrcodeVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const qrCodeContent = ref();
const qrcodeUrl = ref();

const canvasRef = ref();
const queryFormRef = ref<ElFormInstance>();
const shopCashierQrcodeFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ShopCashierQrcodeForm = {
  id: undefined,
  shopId: undefined,
  cashier: undefined,
  accountKey: undefined,
  accountName: undefined,
  qrcode: undefined,
  qrcodeType: undefined,
  qrcodeNote: undefined,
  faceValue: undefined,
  usableTimes: undefined,
  status: undefined,
}
const data = reactive<PageData<ShopCashierQrcodeForm, ShopCashierQrcodeQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    cashier: undefined,
    accountKey: undefined,
    accountName: undefined,
    qrcode: undefined,
    qrcodeType: undefined,
    qrcodeNote: undefined,
    faceValue: undefined,
    usableTimes: undefined,
    status: undefined,
    params: {
    }
  },
  rules: {
    accountKey: [
      { required: true, message: "账号标识不能为空", trigger: "blur" }
    ],
    accountName: [
      { required: true, message: "账号名称不能为空", trigger: "blur" }
    ],
    qrcode: [
      { required: true, message: "二维码不能为空", trigger: "blur" }
    ],
    faceValue: [
      { required: true, message: "面值金额不能为空", trigger: "blur" }
    ],
    usableTimes: [
      { required: true, message: "日使用限制不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商店收银员二维码列表 */
const getList = async () => {
  loading.value = true;
  const res = await listShopCashierQrcode(queryParams.value);
  shopCashierQrcodeList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}


/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  shopCashierQrcodeFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: ShopCashierQrcodeVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 删除按钮操作 */
const handleDelete = async (row?: ShopCashierQrcodeVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除商店收银员二维码编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delShopCashierQrcode(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('retail/shopCashierQrcode/export', {
    ...queryParams.value
  }, `shopCashierQrcode_${new Date().getTime()}.xlsx`)
}

const showQrcode = (row: ShopCashierQrcodeVO) => {
  dialog.visible = true;
  dialog.title = "查看二维码";
  qrcodeUrl.value = row.qrcode;
  qrCodeContent.value = row.qrcodeImage;
  /**
  nextTick(() => {
    const canvas = document.getElementById('canvas');
    qrcode.toCanvas(canvas, row.qrcode);
  });
    **/
}

onMounted(() => {
  getList();
});
</script>
